package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.*;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.SpecParams;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.GoodsMapper;
import com.leyou.item.service.GoodsService;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class GoodServiceImpl extends ServiceImpl<GoodsMapper, Spu> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private SpecParamService specParamService;

    /**
     * 分页查询spu
     *
     * @param brandId    品牌id
     * @param categoryId 商品分类id
     * @param id         商品spu的id
     * @param page       当前页码
     * @param rows       每页大小
     * @param saleable   是否上架
     * @return
     */
    @Override
    public PageSpuDTO goodsPage(Long brandId, Long categoryId, Long id, Long page, Long rows, Boolean saleable) {
        //创建分页对象
        IPage<Spu> spuIPage = new Page<>(page, rows);
        goodsMapper.selectPage(spuIPage, new QueryWrapper<Spu>().eq(null != brandId, "brand_id", brandId)
                .eq(null != categoryId, "cid3", categoryId)
                .eq(null != id, "id", id)
                .eq(null != saleable, "saleable", saleable)
        );

        return new PageSpuDTO(spuIPage.getRecords(), spuIPage.getTotal(), spuIPage.getPages());
    }

    /**
     * 新增商品
     *
     * @param spuDTO
     */
    @Override
    public void insertGood(SpuDTO spuDTO) {
        //Sku类型转换并新增
        List<SkuDTO> skuDTOS = spuDTO.getSkus();
        List<Sku> skuList = new ArrayList<>();
        for (SkuDTO skuDTO : skuDTOS) {
            skuList.add(skuDTO.toEntity(Sku.class));
        }
        skuService.saveBatch(skuList);

        //SpuDetail类型转换并新增
        SpuDetailDTO spuDetail = spuDTO.getSpuDetail();
        spuDetailService.save(spuDetail.toEntity(SpuDetail.class));

        //
        this.save(spuDTO.toEntity(Spu.class));
    }


    @Override
    public void updateGoog(SpuDTO spuDTO) {
        //修改spu表数据
        this.updateById(spuDTO.toEntity(Spu.class));

        //修改sku表数据
        List<SkuDTO> skuDTOList = spuDTO.getSkus();
        for (SkuDTO skuDTO : skuDTOList) {
            Sku sku = skuDTO.toEntity(Sku.class);
            boolean b = skuService.updateById(sku);
            System.out.println(b);
        }

        //
        spuDetailService.update(spuDTO.getSpuDetail().toEntity(SpuDetail.class), new QueryWrapper<SpuDetail>().eq("spu_id", spuDTO.getSpuDetail().getSpuId()));
    }


    /**
     * 根据id集合查询sku集合
     *
     * @param ids
     * @return
     */
    @Override
    public List<SkuDTO> selectSkusByIds(List<Long> ids) {
        return SkuDTO.convertEntityList(this.listByIds(ids));
    }

    /**
     * 根据spuid查询sku集合
     *
     * @param sid
     * @return
     */
    @Override
    public List<SkuDTO> selectSkusById(Long sid) {
        return SkuDTO.convertEntityList(this.query().eq("spu_id", sid).list());
    }

    /**
     * 根据id查询商品详情
     *
     * @param id
     * @return
     */
    @Override
    public SpuDetailDTO selectSpuDetailById(Long id) {
        return new SpuDetailDTO(spuDetailService.query().eq("spu_id", id).one());
    }


    /**
     * 根据spuId查询spu的所有规格参数值
     *
     * @param id        spuid
     * @param searching searching
     * @return
     */
    @Override
    public List<SpecParamDTO> selectSpecValueById(Long id, Boolean searching) {
        Spu spu = this.getById(id);

        SpuDetailDTO spuDetailDTO = selectSpuDetailById(id);
        String specification = spuDetailDTO.toEntity(SpuDetail.class).getSpecification();
        Map<Long, Object> jsonmap = JsonUtils.nativeRead(specification, new TypeReference<Map<Long, Object>>() {
        });

        List<SpecParams> list = specParamService.query().eq("category_id", spu.getCid3()).list();
        List<SpecParamDTO> specParamDTOS = SpecParamDTO.convertEntityList(list);
        for (SpecParamDTO specParamDTO : specParamDTOS) {
            specParamDTO.setValue(jsonmap.get(specParamDTO.toEntity(SpecParams.class).getId()));
        }

        return specParamDTOS;
    }

    /**
     *修改商品上下架
     * @param id
     * @param saleable
     */
    @Override
    public void updateSpuSaleableById(Long id, Boolean saleable) {
       this.update(new UpdateWrapper<Spu>().eq("id",id).set("saleable",saleable));
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public SpuDTO selectSpuSkuSpudetailById(Long id) {
        SpuDTO spuDTO = new SpuDTO(this.getById(id));

        List<SkuDTO> dtoList = SkuDTO.convertEntityList(skuService.query().eq("spu_id", id).list());
        spuDTO.setSkus(dtoList);

        SpuDetail one = spuDetailService.query().eq("spu_id",id).one();
        spuDTO.setSpuDetail(new SpuDetailDTO(one));
        return spuDTO;
    }

    /**
     *根据id查询商品
     * @param id
     * @return
     */
    @Override
    public SpuDTO selectSpuById(Long id) {
        return new SpuDTO(this.getById(id));
    }
}


